// pages/my/mywallet/mywallet.js
Page({
  data: {
    cash: "",
    validBalance: "",
    type: 1,
    values: [{
      type: 1,
      value: 1
    }, {
      type: 2,
      value: 5
    }, {
      type: 3,
      value: 10
    }, {
      type: 4,
      value: 20
    }, {
      type: 5,
      value: 50
    }, {
      type: 6,
      value: 100
    }],
    value: 1,
    fee: "",
    dialog_cash: false,
    paypwd: "",
    show_wx: false
  },
  onLoad: function (options) {
    this.getMyCash();
  },
  onShow: function () {

  },
  getMyCash() {
    getApp().globalData.http.myCashTake().then(res => {
      this.setData({
        cash: res.data.balance,
        validBalance: res.data.validBalance
      });
    });
  },
  toGetCash() {
    wx.navigateTo({
      url: "/pages/my/getcash/getcash",
    })
  },
  toGetDeposit() {
    wx.navigateTo({
      url: "/pages/my/mydeposit/mydeposit",
    })
  },
  selVal(e) {
    this.setData({
      value: e.currentTarget.dataset.value,
      type: e.currentTarget.dataset.type,
    });
  },
  close() {
    this.setData({
      dialog_cash: false
    });
  },
  submit() {
    if (this.data.value > this.data.validBalance) {
      getApp().globalData.vant.toast.fail("账户可提现金额不足");
      return;
    }
    getApp().globalData.http.pwdStatus().then(res => {
      if (res.data.status == 0) {
        getApp().globalData.vant.dialog.confirm({
            title: "设置支付密码",
            message: "首次提现，请设置提现密码，请保存好密码，切勿泄露! ",
            closeOnClickOverlay: true,
            showCancelButton: false,
            confirmButtonText: "去设置",
            confirmButtonColor: "#33B868"
          })
          .then(() => {
            wx.navigateTo({
              url: "/pages/my/walletpasswordset/walletpasswordset",
            })
          })
          .catch(() => {});
      } else {
        this.setData({
          dialog_cash: true,
        });
        this.setData({
          fee: this.data.value * 0.001 < 0.1 ? 0.10 : (this.data.value * 0.001).toFixed(2)
        })
      }
    });
  },
  pwdInput(e) {
    this.setData({
      paypwd: e.detail.value
    });
    if (e.detail.value.length == 6) {
      this.setData({
        dialog_cash: false,
        paypwd: ""
      });
      getApp().globalData.vant.toast.loading({
        message: "提现中...",
        duration: 0
      });
      getApp().globalData.http.getCash({
        type: this.data.type,
        password: e.detail.value
      }).then(res => {
        this.getMyCash();
        getApp().globalData.vant.toast({
          type: "success",
          message: res.msg,
          duration: 1000,
          onClose: () => {
            this.setData({
              show_wx: true
            });
          },
        });
      }).catch(e => {
        if (e.code == 400) {
          if (e.msg == "提现密码错误") {
            getApp().globalData.vant.toast.clear();
            getApp().globalData.vant.dialog.confirm({
                title: "温馨提示",
                message: "提现密码错误，请重试",
                cancelButtonText: "重试",
                confirmButtonText: "忘记密码",
                confirmButtonColor: "#33B868"
              })
              .then(() => {
                this.setData({
                  paypwd: ""
                }, () => {
                  wx.navigateTo({
                    url: "/pages/my/walletpasswordreset/walletpasswordreset",
                  });
                });
              })
              .catch(() => {
                this.setData({
                  dialog_cash: true,
                  paypwd: ""
                });
              });
          } else {
            getApp().globalData.vant.toast.fail(e.msg);
          }
        } else {
          getApp().globalData.vant.toast.fail(e.msg);
        }
      });
    }
  }
})